<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/8/3
 * Time: 10:50
 */
//目的地
namespace app\admin\controller;
use app\admin\model\DestinationModel;
class Destination extends Base{
    //景点列表
    public function spot_list(){
        $where = [];
        $address = input('address');
        if($address){
            $where['address'] = ['like',"%$address%"];
        }
        $where['type'] = 1;
        $data = DestinationModel::where($where)->paginate(10);
        $this->assign('data',$data);
        $this->assign('address',$address);
        $this->assign('type','景点');
        return $this->fetch('list');
    }
    //基础建筑
    public function architecture_list(){
        $where = [];
        $address = input('address');
        if($address){
            $where['address'] = ['like',"%$address%"];
        }
        $where['type'] = 2;
        $data = DestinationModel::where($where)->paginate(10);
        $this->assign('data',$data);
        $this->assign('address',$address);
        $this->assign('type','基础建筑');
        return $this->fetch('list');
    }
    //创建
    public function add(){
        if(request()->isPost()){
            $data = input();
            if(empty($data) || empty($data['address']) || empty($data['key'])) return $this->error('参数错误');
            //查询目的地是否存在
            $is_cunzai = DestinationModel::where('address',$data['address'])->where('type',$data['type'])->find();
            if($is_cunzai) return $this->error('该目的地已存在');
            //写入数据库
            $destination = new DestinationModel();
            $destination->address = $data['address'];
            $destination->type = $data['type'];
            $destination->key = implode(",",$data['key']);
            $re = $destination->save();
            if($re){
                if($data['type'] ==1){
                    return $this->redirect('/admin/destination/spot_list');
                }else{
                    return $this->redirect('/admin/destination/architecture_list');
                }
            }else{
                return $this->error('添加失败');
            }
        }else{
            return $this->fetch();
        }
    }
    //删除
    public function del(){
        if(request()->isAjax()){
            //接收参数
            $id = input('id');
            if(!$id) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            $re = DestinationModel::destroy($id);
            if($re){
                return json(['code'=>200,'data'=>'','msg'=>'删除成功']);
            }else{
                return json(['code'=>2,'data'=>'','msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //修改
    public function edit(){

        if(request()->isPost()){
            //接收参数
            $data = input();
            if(!$data || empty($data['address']) || empty($data['key'])) return $this->error('参数错误');
            //更新数数据
            //检测该目的地是否存在
            $is_cunzai = DestinationModel::where('id','<>',$data['id'])->where('address',$data['address'])->where('type',$data['type'])->find();
            if($is_cunzai) return $this->error('该目的地已存在');
            $destination = DestinationModel::get($data['id']);
            $destination->address = $data['address'];
            $destination->type = $data['type'];
            $destination->key = implode(",",$data['key']);
            $re = $destination->save();
            if($re){
                if($data['type'] ==1){
                    return $this->redirect('/admin/destination/spot_list');
                }else{
                    return $this->redirect('/admin/destination/architecture_list');
                }
            }else{
                return $this->error('添加失败');
            }
        }else{
            $id = input('id');
            if(!$id) return $this->error('参数错误');
            //查询该目的地的信息
            $data = DestinationModel::get($id);
            $this->assign('data',$data);
            return $this->fetch();
        }

    }
}